package com.lyloou.practice.controller;

import com.lyloou.practice.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author lyloou
 * @date 2020/06/06
 * @desc
 */
@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("date01")
    public ModelAndView date01() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("date", new Date().toString());
        mav.setViewName("success");
        return mav;
    }

    @RequestMapping("date02")
    public String date02(ModelMap map) {
        map.addAttribute("date", new Date().toString());
        System.out.println("DemoController.date02:" + map.getClass());
        return "success";
    }

    @RequestMapping("date03")
    public String date03(Map map) {
        map.put("date", new Date().toString());
        System.out.println("DemoController.date03:" + map.getClass());
        return "success";
    }

    @RequestMapping("date04")
    public String date04(Model m) {
        m.addAttribute("date", new Date().toString());
        System.out.println("DemoController.date04:" + m.getClass());
        return "success";
    }

    /**
     * 绑定日期类型参数
     * 定义一个SpringMVC的类型转换器  接口，扩展实现接口接口，注册你的实现
     *
     * @param birthday
     * @return
     */
    @RequestMapping("/handleBirthday")
    public ModelAndView handle06(Date birthday) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /*
     * restful  get   /demo/handle/15
     */
    @RequestMapping(value = "/handle/{id}", method = {RequestMethod.GET})
    public ModelAndView handleGet(@PathVariable("id") Integer id) {

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }


    /*
     * restful  post  /demo/handle
     */
    @RequestMapping(value = "/handle", method = {RequestMethod.POST})
    public ModelAndView handlePost(String username) {

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }


    /*
     * restful  put  /demo/handle/15/lisi
     */
    @RequestMapping(value = "/handle/{id}/{name}", method = {RequestMethod.PUT})
    public ModelAndView handlePut(@PathVariable("id") Integer id, @PathVariable("name") String username) {

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }


    /*
     * restful  delete  /demo/handle/15
     */
    @RequestMapping(value = "/handle/{id}", method = {RequestMethod.DELETE})
    public ModelAndView handleDelete(@PathVariable("id") Integer id) {

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping("/user")
    @ResponseBody
    public User getUser(@RequestBody User user) {
        System.out.println("收到：" + user);
        user.setName("update: " + user.getName());
        return user;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile uploadFile, HttpSession session) throws IOException {
        String originalFilename = uploadFile.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String name = UUID.randomUUID().toString() + "." + ext;

        String realPath = session.getServletContext().getRealPath("/uploads");
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File folder = new File(realPath + "/" + datePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        uploadFile.transferTo(new File(folder, name));
        return "success";
    }


    @RequestMapping("err")
    @ResponseBody
    public String err() {
        int i = 1 / 0;
        return new String("hi");
    }

    @RequestMapping("/handleRedirect")
    public String handleRedirect(String name, RedirectAttributes redirectAttributes) {
        // 方式1：
//        return "redirect:handle01?name=" + name; // 拼接参数安全性、参数⻓度都有局限

        // 方式2：
        // addFlashAttribute⽅法设置了⼀个flash类型属性，该属性会被暂存到session中，在跳转到⻚⾯之后该属性销毁
        redirectAttributes.addFlashAttribute("name", name);
        return "redirect:handle01";
    }

    @RequestMapping("handle01")
    public ModelAndView handle01(String name) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("date", name);
        mav.setViewName("success");
        return mav;
    }
}
